package servlet;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Answer;
import model.FeedBack;
import DAO.AnswerDao;
import DAO.FeedBackDao;

public class AnswerInfoAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ActionForward forward = new ActionForward();
		HttpSession session = request.getSession();

		int studyId = request.getParameter("studyId") != null
				&& request.getParameter("studyId") != "" ? Integer
				.parseInt(request.getParameter("studyId")) : 0;
		int userIdCheck = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String) session.getAttribute("userid")) : 0;
		int questId = request.getParameter("questId") != null
				&& request.getParameter("questId") != "" ? Integer
				.parseInt(request.getParameter("questId")) : 0;
		int answerId = request.getParameter("answerId") != null
				&& request.getParameter("answerId") != "" ? Integer
				.parseInt(request.getParameter("answerId")) : 0;
		if (userIdCheck < 1) {
			session.invalidate();
			Util.ShowError(response, "로그인해주세요");
		}
		if (studyId < 1 || questId < 1 || answerId < 1) {
			Util.ShowError(response, "잘못 된 접근입니다");
		}
		
		
		AnswerDao answerDao = new AnswerDao();
		Answer answer = answerDao.getAnswerInfo(answerId);
		
		FeedBackDao feedbackDao = new FeedBackDao();
		ArrayList <FeedBack> feedbackList = new ArrayList <FeedBack>();
		
		feedbackList=feedbackDao.getAllFeedBackInfo(answerId);

		
		request.setAttribute("studyId", studyId);
		request.setAttribute("questId", questId);
		request.setAttribute("answer", answer);
		request.setAttribute("feedBackList", feedbackList);

		forward.setPath("./study_quest_showAnswer.jsp");
		return forward;

	}

}
